var Command = require('./public/command.js');

var server = require('http').createServer(function (req, res) {
    res.end('这个是web socket服务');
});

require('./socket.io.js')(server);

var worker;
process.on("message", function (msg, socket) {
    process.nextTick(function () {
        if (msg == 'c' && socket) {
            worker = socket;
            socket.readable = socket.writable = true;
            socket.resume();
            //server.connections++;
            //server.getConnections();
            socket.server = server;
            server.emit("connection", socket);
            socket.emit("connect");
        }
    });
});

process.on('uncaughtException', function(err){

    //日志
    console.log(err);

    //向主进程发送自杀信号
    process.send({act:'suicide'});

    //停止接收新的连接
    worker.close(function(){
        //所有连接断开后,退出进程
        process.exit(1);
    });

    //5秒后退出进程, 当连接的是长连接时，设置定时退出
    setTimeout(function(){
        process.exit(1);
    }, 5000);
});